<?php
// +----------------------------------------------------------------------
// | ZHANGKUI [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.rndchina.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Zhangkui < zk_admin@163.com >
// +----------------------------------------------------------------------
namespace app\admin\controller;

use app\admin\model\ApiCompanyModel;
use app\admin\model\ApiUserModel;
use app\admin\model\UserModel;
use think\Db;

/**
 * Func 操作类
 *
 * Class ApiCompanyController
 *
 * @package app\admin\controller
 */
class ApiCompanyController extends BaseController
{
    /**
     * Func 列表
     */
    public function index()
    {
        // 时间
        if ($this->start_time && $this->end_time)
        {
            $condition['a.create_time'] = ['between', [
                strtotime($this->start_time), strtotime($this->end_time),
            ]];
        }

        // 关键词
       if ($this->keyword)
       {
            if ($this->searchType)
            {
                $condition[$this->searchType] = ['like', '%' . $this->keyword . '%'];
            } else {
                $condition['a.company_title|b.user_login'] = ['like', '%' . $this->keyword . '%'];
            }
        }

        $condition['a.id'] = ['gt', 0];

        // 字段
        $fieldArr = ['a.*', 'b.user_login'];

        // 连接表
        $joinArr = [['user b', 'a.user_id = b.id', 'left']];

        // 数据
        $infos['list'] = (new ApiCompanyModel())->getApiCompanyListInfo(
            $condition, ['a.id' => 'desc'], ['page' => $this->page, 'limit' => $this->limit],
            $fieldArr, $joinArr
        );

        $infos['companyStatusArr'] = self::$companyStatusArr;

        $this->assign('infos', $infos);

        return $this->fetch('index');
    }

    /**
     * Func 添加
     */
    public function add()
    {
        $usobj = new ApiUserModel();

        if ($this->request->isPost())
        {
            $param = $this->request->param();

            $result = $this->validate($param, 'ApiCompany.add');
            if (!is_bool($result))
            {
                $this->error($result);
            }

            // 验证账号是否存在
            $getApiUserOneInfo = $usobj->getApiUserOneInfo(['a.id' => $param['user_id']], ['a.id' => 'desc'], ['a.id']);
            if (!$getApiUserOneInfo) {
                $this->error('管理员不存在');
            }

            // 添加和更新的字段信息
            $data1 = self::getPostParamInfo([
                'company_title', 'company_tel', 'company_desc', 'address',
            ]);

            // 附加参数
            $data2['user_id'] = $getApiUserOneInfo['id']; // 管理员id
            $data2['status'] = session('admin_is_operate') == 1 ? 1 : 3; // 非平台添加需要审核

            // 添加的数据
            $data = array_merge((array)$data1, (array)$data2);

            $coobj = new ApiCompanyModel();

            // 查询数据是否存在
            $conditionCC['a.user_id'] = array('eq', $getApiUserOneInfo['id']);
            $conditionCC['a.company_title'] = array('eq', trim($data['company_title']));
            if ($coobj->getApiCompanyCount($conditionCC))
            {
                $this->error('数据已存在');
            }

            if ($coobj->addOrUpdateApiCompanyInfo($data)) {
                $this->success('添加成功!', url('index', []));
            } else {
                $this->error('添加失败,请稍后重试');
            }
        }

        // 旅行社管理员
        $conditionUL['a.user_status'] = ['eq', 1];
        $conditionUL['a.id'] = ['in', $this->getUserIdArr([3])];
        $detail['getUserListInfo'] = $usobj->getApiUserListInfo(
            $conditionUL, ['a.id' => 'desc'], ['page' => 1, 'limit' => 500], ['a.id', 'a.user_nickname']
        );

        $detail['companyStatusArr'] = self::$companyStatusArr;

        $this->assign('detail', $detail);

        return $this->fetch();
    }

    /**
     * Func 修改
     */
    public function edit()
    {
        $param = $this->request->param();

        $usobj = new ApiUserModel();
        $coobj = new ApiCompanyModel();

        $detail = $coobj->getApiCompanyOneInfo(['uuid' => $param['uuid']], ['id' => 'desc']);
        if (!$detail) $this->error('信息不存在');

        if ($this->request->isPost())
        {
            $result = $this->validate($param, 'ApiCompany.edit');
            if (!is_bool($result))
            {
                $this->error($result);
            }

            // 验证账号是否存在
            $getApiUserOneInfo = $usobj->getApiUserOneInfo(['a.id' => $param['user_id']], ['a.id' => 'desc']);
            if (!$getApiUserOneInfo)
            {
                $this->error('管理员不存在');
            }

            // 添加和更新的字段信息
            $data1 = self::getPostParamInfo([
                'company_title', 'company_tel', 'company_desc', 'address',
            ]);

            // 附加参数
            $data2['user_id'] = $getApiUserOneInfo['id']; // 管理员id

            // 管理員操作状态
            if(session('admin_is_operate') == 1)
            {
                $data2['status'] = $param['status'];
            }

            // 添加的数据
            $data = array_merge((array)$data1, (array)$data2);

            // 查询数据是否存在
            $conditionCC['a.id'] = array('neq', $detail['id']);
            $conditionCC['a.user_id'] = array('eq', $getApiUserOneInfo['id']);
            $conditionCC['a.company_title'] = array('eq', trim($data['company_title']));
            if ($coobj->getApiCompanyCount($conditionCC))
            {
                $this->error('数据已存在');
            }

            // 修改的数据
            $data = array_merge((array)$data1, (array)$data2);

            if ($coobj->addOrUpdateApiCompanyInfo($data, $detail['id'])) {
                $this->success('修改成功!', url('index', []));
            } else {
                $this->error('修改失败,请稍后重试');
            }
        }

        // 旅行社管理员
        $conditionUL['a.user_status'] = ['eq', 1];
        $conditionUL['a.id'] = ['in', $this->getUserIdArr([3])];
        $detail['getUserListInfo'] = $usobj->getApiUserListInfo(
            $conditionUL, ['a.id' => 'desc'], ['page' => 1, 'limit' => 500], ['a.id', 'a.user_nickname']
        );

        $detail['companyStatusArr'] = self::$companyStatusArr;

        $this->assign('detail', $detail);

        return $this->fetch();
    }

}